package Solution101_200.problem116.PopulatingNextRightPointersInEachNode;

import common.Node;

/**
 * @program Leetcode
 * @description:
 * @author: lishangsheng
 * @create: 2019/07/17 09:54
 */
public class Solution {
    public Node connect(Node root) {
        if(root==null){
            return null;
        }
        root.next=null;
        connectNode(root);
        return root;
    }

    private void connectNode(Node root){
        if(root.left==null||root.right==null){
            return;
        }
        root.left.next=root.right;
        if(root.next==null){
            root.right.next=null;
        }else {
            root.right.next = root.next.left;
        }
        connectNode(root.left);
        connectNode(root.right);
    }
}
